//
//  CustomHomePopUpScrollView.m
//  YOYOLive
//
//  Created by 王根强 on 16/9/19.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "CustomHomePopUpScrollView.h"

@interface CustomHomePopUpScrollView ()<UIScrollViewDelegate> {
    UIView * titleView;
    UIButton * selectButton;
    UIView * sliderView;
    UIImageView *imageView;//三角图片
    NSInteger baseTag;
    BOOL selectedBool;
}
@property (nonatomic, strong) NSMutableArray * buttonArray;

@end

@implementation CustomHomePopUpScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.buttonArray = [NSMutableArray array];
    }
    return self;
}

- (void)buildWithTitleArray:(NSArray *)titleArray
        withTitleViewHeight:(CGFloat)titleHeight
              withTitleFont:(UIFont *)titleFont
             withTitleColor:(NSArray *)titleColorArray
              withButtonTag:(NSInteger)tags
  withLittleScrollViewColor:(UIColor *)viewColor
{
    titleView = [[UIView alloc] initWithFrame:CGRectMake(59, 0, self.width-118, titleHeight)];
    titleView.backgroundColor = NAV_COLOR_BLUE;
    [self addSubview:titleView];
    baseTag = tags;
    for (int i = 0; i < titleArray.count; i++) {
        UIButton * titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(titleView.width/titleArray.count*i, 10, titleView.width/titleArray.count, titleHeight-20);
        [titleButton setTitle:titleArray[i] forState:UIControlStateNormal];
        titleButton.titleLabel.font = titleFont;
        [titleButton setTitleColor:titleColorArray[0] forState:UIControlStateNormal];
        [titleButton setTitleColor:titleColorArray[1] forState:(UIControlStateSelected)];
        titleButton.tag = tags+i;
        [titleButton addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:titleButton];
        if (i == 0) {
            selectButton = titleButton;
            selectButton.selected = YES;
        }
        [self.buttonArray addObject:titleButton];
    }
    //滑块
    UIView * sliderV=[[UIView alloc]initWithFrame:CGRectMake(0, titleView.height-7, titleView.width/titleArray.count/2, 1)];
    sliderV.centerX = selectButton.centerX;
    sliderV.backgroundColor = viewColor;
    [titleView addSubview:sliderV];
    sliderView = sliderV;
    // scrollView
    self.baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, titleHeight, self.width, self.height-titleHeight)];
    self.baseScrollView.delegate = self;
    self.baseScrollView.pagingEnabled = YES;
    self.baseScrollView.scrollEnabled = YES;
    self.baseScrollView.showsHorizontalScrollIndicator = NO;
    self.baseScrollView.showsVerticalScrollIndicator = NO;
    self.baseScrollView.contentSize = CGSizeMake(self.width*titleArray.count, self.baseScrollView.height);
    [self addSubview:self.baseScrollView];

    //中间向下展开图片
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(selectButton.width + selectButton.width/2-6, titleView.height-10, 12, 5)];
    imageView.tag = 10051;//tag值需要修改
    imageView.image = [UIImage imageNamed:@"remen"];
}

- (void)scrollViewSelectToIndex:(UIButton *)button {
    //进到中间的界面第一次不弹出，再点一次弹出
    if (button.tag-baseTag == 1) {
        if (selectedBool == YES) {
            //发送弹出框通知
            [[NSNotificationCenter defaultCenter]postNotificationName:@"pop-up_Notification" object:self];
        }
        selectedBool = YES;
    }else {
        selectedBool = NO;
    }
    [self selectButton:button.tag-baseTag];
    [UIView animateWithDuration:0.3 animations:^{
        self.baseScrollView.contentOffset = CGPointMake(self.width * (button.tag-baseTag), 0);
    }];
    
    //发送点击标题按钮通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"selectedTitleBtn_Notification" object:self userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",button.tag-baseTag],@"index", nil]];
}
//选择某个标题
- (void)selectButton:(NSInteger)index {
    selectButton.selected = NO;
    selectButton = self.buttonArray[index];
    selectButton.selected = YES;
    if (index == 1) {
        sliderView.hidden = YES;
        [titleView addSubview:imageView];
    }else {
        sliderView.hidden = NO;
        UIView *tagView = [titleView viewWithTag:10051];
        [tagView removeFromSuperview];
    }
    [UIView animateWithDuration:0.3 animations:^{
        sliderView.frame = CGRectMake(selectButton.width*index, titleView.height-7, selectButton.width/2, 1);
        sliderView.centerX = selectButton.centerX;
    }];
}

//监听滚动事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / self.width;
    if (index != 1) {
        selectedBool = NO;
    }
    [self selectButton:index];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / self.width;
    //发送点击标题按钮通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"selectedTitleBtn_Notification" object:self userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",index],@"index", nil]];
}



@end
